Ext.define('SKJF.controller.TERIMABARANG',{
	extend: 'Ext.app.Controller',
	views: ['PEMBELIAN.v_terimabarang','PEMBELIAN.v_terimabarang_form'],
	models: ['m_terimabarang'],
	stores: ['s_terimabarang'],
	
	requires: ['Ext.ModelManager'],
	
	refs: [{
		ref: 'Listterimabarang',
		selector: 'Listterimabarang'
	}, {
		ref: 'Listterimabarangdet',
		selector: 'Listterimabarangdet'
	}, {
		ref: 'v_terimabarang_form',
		selector: 'v_terimabarang_form'
	}, {
		ref: 'Saveterimabarang',
		selector: 'v_terimabarang_form #save'
	}, {
		ref: 'Createterimabarang',
		selector: 'v_terimabarang_form #create'
	}, {
		ref: 'TERIMABARANG',
		selector: 'TERIMABARANG #center'
	}],


	init: function(){
		this.control({
			'TERIMABARANG': {
				'afterrender': this.terimabarangAfterRender
			},
			'v_terimabarang_form': {
				'afterlayout': this.terimabarangAfterLayout
			},
			'Listterimabarang': {
				'selectionchange': this.enableDelete,
				'itemdblclick': this.updateListterimabarang
			},
			'Listterimabarang button[action=create]': {
				click: this.createRecord
			},
			'Listterimabarang button[action=delete]': {
				click: this.deleteRecord
			},
			'Listterimabarang button[action=print]': {
				click: this.printRecords
			},
			'v_terimabarang_form button[action=save]': {
				click: this.saveV_terimabarang_form
			},
			'v_terimabarang_form button[action=create]': {
				click: this.saveV_terimabarang_form
			},
			'v_terimabarang_form button[action=cancel]': {
				click: this.cancelV_terimabarang_form
			}
		});
	},
	
	terimabarangAfterRender: function(){
		var terimabarangStore = this.getListterimabarang().getStore();
		terimabarangStore.load();
	},
	
	terimabarangAfterLayout: function(){
		this.getV_terimabarang_form().down('#NOMOR_PO_field').focus(false, true);
	},
	
	createRecord: function(){
		var getListterimabarang	= this.getListterimabarang();
		var getV_terimabarang_form= this.getV_terimabarang_form(),
			form			= getV_terimabarang_form.getForm();
		var getSaveterimabarang	= this.getSaveterimabarang();
		var getCreateterimabarang	= this.getCreateterimabarang();
		
		/* grid-panel */
		getListterimabarang.setDisabled(true);
        
		/* form-panel */
		form.reset();
		getV_terimabarang_form.down('#NOMOR_LPB_field').setReadOnly(true);
		getSaveterimabarang.setDisabled(true);
		getCreateterimabarang.setDisabled(false);
		getV_terimabarang_form.setDisabled(false);
		
		this.getTERIMABARANG().setActiveTab(getV_terimabarang_form);		
	},
	
	enableDelete: function(dataview, selections){
		if (selections.length) {
			var sel = selections[0].data;
			// console.info('Sel : '+sel.NOMOR_LPB);
			this.getListterimabarang().down('#btndelete').setDisabled(!selections.length);
			this.getListterimabarangdet().down('#btncreate').setDisabled(false);
			this.getListterimabarangdet().down('#btnprint').setDisabled(false);
			
			this.getListterimabarangdet().getStore().load({
				params: {
					NOMOR_LPB: sel.NOMOR_LPB
				}
			});
			
		}else{
			this.getListterimabarang().down('#btndelete').setDisabled(!selections.length);
			this.getListterimabarangdet().down('#btncreate').setDisabled(true);
			this.getListterimabarangdet().down('#btndelete').setDisabled(true);
			this.getListterimabarangdet().down('#btnprint').setDisabled(true);
			this.getListterimabarangdet().getStore().removeAll();
		}
	},
	
	updateListterimabarang: function(me, record, item, index, e){
		var getTERIMABARANG		= this.getTERIMABARANG();
		var getListterimabarang	= this.getListterimabarang();
		var getV_terimabarang_form= this.getV_terimabarang_form(),
			form			= getV_terimabarang_form.getForm();
		var getSaveterimabarang	= this.getSaveterimabarang();
		var getCreateterimabarang	= this.getCreateterimabarang();
		
		getSaveterimabarang.setDisabled(false);
		getCreateterimabarang.setDisabled(true);
		getV_terimabarang_form.down('#NOMOR_LPB_field').setReadOnly(true);		
		getV_terimabarang_form.loadRecord(record);
		
		getListterimabarang.setDisabled(true);
		getV_terimabarang_form.setDisabled(false);
		getTERIMABARANG.setActiveTab(getV_terimabarang_form);
	},
	
	deleteRecord: function(dataview, selections){
		var getstore = this.getListterimabarang().getStore();
		var selection = this.getListterimabarang().getSelectionModel().getSelection()[0];
		if(selection){
			Ext.Msg.confirm('Confirmation', 'Are you sure to delete this data: "NOMOR_LPB" = "'+selection.data.NOMOR_LPB+'","ID" = "'+selection.data.ID+'"?', function(btn){
				if (btn == 'yes'){
					getstore.remove(selection);
					getstore.sync();
				}
			});
			
		}
	},
	
	printRecords: function(){
		var getstore = this.getListterimabarang().getStore();
		var jsonData = Ext.encode(Ext.pluck(getstore.data.items, 'data'));
		
		Ext.Ajax.request({
			method: 'POST',
			url: 'c_terimabarang/printRecords',
			params: {data: jsonData},
			success: function(response){
				var result=eval(response.responseText);
				switch(result){
				case 1:
					win = window.open('./temp/terimabarang.html','terimabarang_list','height=400,width=900,resizable=1,scrollbars=1, menubar=1');
					break;
				default:
					Ext.MessageBox.show({
						title: 'Warning',
						msg: 'Unable to print the grid!',
						buttons: Ext.MessageBox.OK,
						animEl: 'save',
						icon: Ext.MessageBox.WARNING
					});
					break;
				}  
			}
		});
	},
	
	saveV_terimabarang_form: function(){
		var getTERIMABARANG		= this.getTERIMABARANG();
		var getListterimabarang 	= this.getListterimabarang();
		var getV_terimabarang_form= this.getV_terimabarang_form(),
			form			= getV_terimabarang_form.getForm(),
			values			= getV_terimabarang_form.getValues();
		var store 			= this.getStore('s_terimabarang');
		
		if (form.isValid()) {
			var jsonData = Ext.encode(values);
			
			Ext.Ajax.request({
				method: 'POST',
				url: 'c_terimabarang/save',
				params: {data: jsonData},
				success: function(response){
					store.reload({
						callback: function(){
							var newRecordIndex = store.findBy(
								function(record, id) {
									if (record.get('NOMOR_LPB') === values.NOMOR_LPB) {
										return true;
									}
									return false;
								}
							);
							/* getListterimabarang.getView().select(recordIndex); */
							getListterimabarang.getSelectionModel().select(newRecordIndex);
						}
					});
					
					getV_terimabarang_form.setDisabled(true);
					getListterimabarang.setDisabled(false);
					getTERIMABARANG.setActiveTab(getListterimabarang);
				}
			});
		}
	},
	
	createV_terimabarang_form: function(){
		var getTERIMABARANG		= this.getTERIMABARANG();
		var getListterimabarang 	= this.getListterimabarang();
		var getV_terimabarang_form= this.getV_terimabarang_form(),
			form			= getV_terimabarang_form.getForm(),
			values			= getV_terimabarang_form.getValues();
		var store 			= this.getStore('s_terimabarang');
		
		if (form.isValid()) {
			var jsonData = Ext.encode(values);
			
			Ext.Ajax.request({
				method: 'POST',
				url: 'c_terimabarang/save',
				params: {data: jsonData},
				success: function(response){
					store.reload();
					
					getV_terimabarang_form.setDisabled(true);
					getListterimabarang.setDisabled(false);
					getTERIMABARANG.setActiveTab(getListterimabarang);
				}
			});
		}
	},
	
	cancelV_terimabarang_form: function(){
		var getTERIMABARANG		= this.getTERIMABARANG();
		var getListterimabarang	= this.getListterimabarang();
		var getV_terimabarang_form= this.getV_terimabarang_form(),
			form			= getV_terimabarang_form.getForm();
			
		form.reset();
		getV_terimabarang_form.setDisabled(true);
		getListterimabarang.setDisabled(false);
		getTERIMABARANG.setActiveTab(getListterimabarang);
	}
	
});